//
//  DetailBelleViewController.m
//  LOL_JewelBox
//
//  Created by dllo on 15/4/27.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DetailBelleViewController.h"
#import "GMDCircleLoader.h"
#import "RNFrostedSidebar.h"
#import <ShareSDK/ShareSDK.h>
#import "CollectModel.h"
@interface DetailBelleViewController ()<RNFrostedSidebarDelegate>
{
    UIWebView *webView;
}
@property(nonatomic, strong)NSMutableIndexSet *optionIndices;
@property(nonatomic, copy)NSString *tempUrl;
@property(nonatomic, retain)NSMutableArray *collectArr;
@end

@implementation DetailBelleViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
       self.optionIndices = [NSMutableIndexSet indexSet];
        self.collectArr = [NSMutableArray array];
    }
    return self;
}

-(void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    //创建网络请求
    self.tempUrl = [NSString stringWithFormat:@"http://lol.data.shiwan.com/getArticleInfo/loldata?article_id=%@", self.id];
    
    NSURL *url = [NSURL URLWithString:self.tempUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT - 64)];
    webView.backgroundColor = [UIColor whiteColor];
    //设置WebView的代理
    webView.delegate = self;
    //伸缩内容屏幕尺寸
    webView.scalesPageToFit = NO;
    //加载网络请求
    [webView loadRequest:request];
    [self.view addSubview:webView];
    [webView release];
    
     [GMDCircleLoader setOnView:self.view withTitle:@"Loading..." animated:YES];
    
//    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"shoucang"] style:UIBarButtonItemStylePlain target:self action:@selector(Click:)];
}

//deskAction抽屉效果

-(void)Click:(id)sender
{
    
    NSArray *images = @[[UIImage imageNamed:@"分享"],[UIImage imageNamed:@"收藏"]];
    
    NSArray *colors = @[[UIColor colorWithRed:240/255.f green:159/255.f blue:254/255.f alpha:1],
                        [UIColor colorWithRed:255/255.f green:137/255.f blue:167/255.f alpha:1]];
    
    
    RNFrostedSidebar *callout = [[RNFrostedSidebar alloc] initWithImages:images selectedIndices:self.optionIndices borderColors:colors];
    callout.borderWidth = 5;
    callout.delegate = self;
    [callout show];
    
}

// 抽屉效果点击事件
- (void)sidebar:(RNFrostedSidebar *)sidebar didTapItemAtIndex:(NSUInteger)index
{
    // 新建视图 进行补充
    if (index == 0) {
        NSString *imagePath = [[NSBundle mainBundle]pathForResource:@"ShareSDK" ofType:@"png"];
        //构造分享内容
        id<ISSContent> publishContent = [ShareSDK content:self.tempUrl
                                           defaultContent:@"测试一下"
                                                    image:[ShareSDK imageWithPath:imagePath]
                                                    title:@"ShareSDK"
                                                      url:@"http://www.mob.com"
                                              description:@"这是一条测试信息"
                                                mediaType:SSPublishContentMediaTypeNews];
        //创建弹出菜单容器
        id<ISSContainer> container = [ShareSDK container];
        //        [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
        
        //弹出分享菜单
        [ShareSDK showShareActionSheet:container
                             shareList:nil
                               content:publishContent
                         statusBarTips:YES
                           authOptions:nil
                          shareOptions:nil
                                result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                    
                                    if (state == SSResponseStateSuccess)
                                    {
                                        NSLog(NSLocalizedString(@"TEXT_ShARE_SUC", @"分享成功"));
                                    }
                                    else if (state == SSResponseStateFail)
                                    {
                                        NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
                                    }
                                }];
    }else if (index == 1)
        {
            
            CollectModel *collectMo = [[CollectModel alloc]init];
            collectMo.title = self.title;
            collectMo.url = self.tempUrl;
            
            NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *sandBoxPath = [sandBox lastObject];
            NSFileManager *manager = [NSFileManager defaultManager];
            NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"收藏"];
            [manager createDirectoryAtPath:documentPath withIntermediateDirectories:YES attributes:nil error:nil];
            NSString *documentPathstr = [documentPath stringByAppendingPathComponent:@"collectBelle.plist"];
            NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:documentPathstr];
            NSInteger i = 0;
            for (CollectModel *temp  in array) {
                if ([collectMo.url isEqualToString:temp.url]) {
                    i++;
                }
            }
            if (i == 0) {
                for (NSInteger i = 0; i < array.count; i++) {
                    [self.collectArr addObject:array[i]];
                }
                [self.collectArr addObject:collectMo];
                [NSKeyedArchiver archiveRootObject:self.collectArr toFile:documentPathstr];
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"返回", nil];
                [alert show];
                [alert release];
            }else
            {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:@"返回", nil];
                [alert show];
                [alert release];
            }
            
            [collectMo release];
            [sidebar dismiss];
        }
}

- (void)sidebar:(RNFrostedSidebar *)sidebar didEnable:(BOOL)itemEnabled itemAtIndex:(NSUInteger)index
{
    if (itemEnabled) {
        [self.optionIndices addIndex:index];
    }
    else {
        [self.optionIndices removeIndex:index];
    }
}

//WebView开始加载资源的时候调用(开始发送请求)
-(void)webViewDidStartLoad:(UIWebView *)webView
{
//    [activityIndicatorView startAnimating];
}

//WebView加载完毕的时候调用(请求完毕)

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//    [activityIndicatorView stopAnimating];
    [GMDCircleLoader hideFromView:self.view animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
